package com.wx.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.eova.common.Easy;
import com.eova.core.IndexController;
import com.eova.interceptor.AuthInterceptor;
import com.eova.interceptor.LoginInterceptor;
import com.jfinal.aop.Clear;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.wx.core.Constants;
import com.wx.model.PartyPrizeDetail;
import com.wx.service.MarketingService;
import com.wx.sql.MarketingSql;
@Clear({LoginInterceptor.class,AuthInterceptor.class})
public class MarketingController extends IndexController{
	static final MarketingService marketingService = MarketingService.me;

	@Override
	public void index(){
		setAttr("AA", "aa");
		renderTemplate("/wx/choujianng/index.html");
	}
	/**
	 * @Description: 获得次数
	 * @param    
	 * @return void  
	 * @throws
	 * @author liuxin
	 * @date 2018年5月21日
	 */
	public void getNumberOfAwards(){
		Easy easy = new Easy();
		String msg = "";
		if( getRequest().getParameter("USER_SOURCE") == null){
			msg += "用户来源为空;";
		}
		if(getRequest().getParameter("USER_TYPE") == null){
			msg += "用户类型为空;";
		}
		if(getRequest().getParameter("USER_CODE") == null){
			msg += "用户编码为空;";
		}
		if(getRequest().getParameter("LOGIN_NUM") == null){
			msg += "登录次数为空;";
		}
		if(getRequest().getParameter("ORDER_NUM") == null){
			msg += "下单次数为空;";
		}
		if(getRequest().getParameter("REPEAT_NUM") == null){
			msg += "转发次数为空;";
		}
		if(getRequest().getParameter("PARTY_ID") == null){
			msg += "活动ID为空;";
		}
		if(!"".equals(msg)){

			easy.setMsg(msg);
			easy.setSuccess(true);
		}
		try {
			Map<String, Object> map = new HashMap<String, Object>(7);
			map.put("USER_SOURCE", getRequest().getParameter("USER_SOURCE"));
			map.put("USER_TYPE", getRequest().getParameter("USER_TYPE"));
			map.put("USER_CODE", getRequest().getParameter("USER_CODE"));
			map.put("LOGIN_NUM", getRequest().getParameter("LOGIN_NUM"));
			map.put("ORDER_NUM",getRequest().getParameter("ORDER_NUM"));
			map.put("REPEAT_NUM", getRequest().getParameter("REPEAT_NUM"));
			map.put("PARTY_ID", getRequest().getParameter("PARTY_ID"));
			renderJson(marketingService.getNumberOfAwards(map));
			setAttr("USER_CODE", map.get("USER_CODE"));
			setAttr("PARTY_ID", map.get("PARTY_ID"));
			renderTemplate("/wx/choujianng/index.html");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/**
	 * @Description: 获取用户所得奖励
	 * @param    
	 * @return void  
	 * @throws
	 * @author liuxin
	 * @date 2018年5月21日
	 */
	public void getAwards(){
		Easy easy = new Easy();
		String msg = "";
		String USER_CODE = getRequest().getParameter("USER_CODE");
		String PARTY_ID = getRequest().getParameter("PARTY_ID");
		String key = "getAwards" + USER_CODE;
		if(Constants.controlMap.containsKey(key)){

		}else{
			Constants.controlMap.put(key,key);
			if( USER_CODE == null){
				msg += "用户编码为空;";
			}
			if(PARTY_ID == null){
				msg += "活动ID为空;";
			}
			if(!"".equals(msg)){
				easy.setMsg(msg);
				easy.setSuccess(true);
			}else{
				try {
					Map<String, Object> map = new HashMap<String, Object>(2);
					map.put("USER_CODE", USER_CODE);
					map.put("PARTY_ID", PARTY_ID);
					easy = marketingService.getAReward(map);
				} catch (Exception e) {
					easy.setMsg("服务器异常");
					easy.setSuccess(false);
					e.printStackTrace();
				}finally {
					Constants.controlMap.remove(key);
				}
			}
			renderJson(easy);
		}
	}

	/**
	 * @Description: 获取该活动参与抽奖的奖励
	 * @param    
	 * @return void  
	 * @throws
	 * @author liuxin
	 * @date 2018年5月21日
	 */
	public void getAwardDetail(){
		Easy easy = new Easy();
		String msg = "";
		if(getRequest().getParameter("PARTY_ID") == null){
			msg += "活动ID为空;";
		}
		if(!"".equals(msg)){
			easy.setMsg(msg);
			easy.setSuccess(true);
		}else{
			List<PartyPrizeDetail> prizeList = PartyPrizeDetail.dao.find(MarketingSql.getAwardDetail(Integer.parseInt(getRequest().getParameter("PARTY_ID"))));
			easy.setMsg("SUCCESS");
			easy.setData(prizeList);
			easy.setSuccess(true);
		}
		renderJson(easy);
	}



	public void myAward(){
		//获取用户基本信息
		String party_id = getPara("party_id");
		String user_id = getPara("user_id");
		List<Record> partyPrizeRecords = Db.find(MarketingSql.getAwardRecord(),party_id,user_id);//Db.find(MarketingSql.getAwardRecord(),party_id,user_id);
		setAttr("partyPrizeRecords", JSON.toJSON(partyPrizeRecords));
		renderTemplate("/wx/choujianng/award/myAward.html");
	}

}